package com.gapp.king.web.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.gapp.king.web.client.DelistedException;
import com.gapp.king.web.client.GappWebService;
import com.gapp.king.web.client.StockPrice;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class StockPriceServiceImpl extends RemoteServiceServlet implements GappWebService {
	
	private static final double MAX_PRICE = 100.0;
	private static final double MAX_PRICE_CHANGE = 0.02;

	@Override
	public List<StockPrice> getPrices(String[] symbols) throws DelistedException{
		// TODO Auto-generated method stub
		Random rnd = new Random();
		List<StockPrice> prices = new ArrayList<StockPrice>();
        for (int i = 0; i < symbols.length; i++) 
        {
          if (symbols[i].equals("ERR")) 
          {
             throw new DelistedException("ERR");
          }
        	
          double price = rnd.nextDouble() * MAX_PRICE;
          double change = price * MAX_PRICE_CHANGE
              * (rnd.nextDouble() * 2.0 - 1.0);

          prices.add(new StockPrice(symbols[i], price, change));
        }


	    return prices;
	}
}
